package bean;

import com.sun.xml.bind.v2.schemagen.episode.Bindings;

import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;


import javax.faces.event.ValueChangeEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.share.ADFContext;
import oracle.adf.share.security.SecurityContext;
import oracle.adf.view.rich.component.rich.RichPopup;

import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;
import oracle.adf.view.rich.component.rich.nav.RichCommandImageLink;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.DialogEvent.Outcome;

import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.Row;

import org.apache.myfaces.trinidad.model.RowKeySet;

import util.ADFUtil;


public class ProjectDetailsBean {
    private RichPopup membersPopup;
    private RichPopup projectPopup;
    private RichTable projectsTable;
    private RichPopup deleteProjectPopup;
    private RichTable membersTable;
    private RichPopup deleteMemberPopup;
    private RichInputText comments;
    private RichPopup commentsPopup;
    private RichCommandImageLink addTask;


    /**
     * This is a constructor
     */
    public ProjectDetailsBean() {
    }


    public BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public void setProjectPopup(RichPopup projectPopup) {
        this.projectPopup = projectPopup;
    }

    public RichPopup getProjectPopup() {
        return projectPopup;
    }

    public void setProjectsTable(RichTable projectsTable) {
        this.projectsTable = projectsTable;
    }

    public RichTable getProjectsTable() {
        return projectsTable;
    }

    public void setDeleteProjectPopup(RichPopup deleteProjectPopup) {
        this.deleteProjectPopup = deleteProjectPopup;
    }

    public RichPopup getDeleteProjectPopup() {
        return deleteProjectPopup;
    }

    public void setMembersPopup(RichPopup membersPopup) {
        this.membersPopup = membersPopup;
    }

    public RichPopup getMembersPopup() {
        return membersPopup;
    }

    public void setMembersTable(RichTable membersTable) {
        this.membersTable = membersTable;
    }

    public RichTable getMembersTable() {
        return membersTable;
    }

    public void setDeleteMemberPopup(RichPopup deleteMemberPopup) {
        this.deleteMemberPopup = deleteMemberPopup;
    }

    public RichPopup getDeleteMemberPopup() {
        return deleteMemberPopup;
    }


    public void setComments(RichInputText comments) {
        this.comments = comments;
    }

    public RichInputText getComments() {
        return comments;
    }

    public void setCommentsPopup(RichPopup commentsPopup) {
        this.commentsPopup = commentsPopup;
    }

    public RichPopup getCommentsPopup() {
        return commentsPopup;
    }


    /**
     *
     * @param actionEvent
     */
    public void updateProject(ActionEvent actionEvent) {


        ADFUtil.setEL("#{viewScope.operation}", "update");

        String sourceType = (String)ADFContext.getCurrent().getPageFlowScope().get("type");
        oracle.jbo.domain.Number sourceId =
            (oracle.jbo.domain.Number)ADFContext.getCurrent().getPageFlowScope().get("sourceId");
        if (sourceType != null && sourceId != null) {
            BindingContainer bindings = getBindings();
            OperationBinding operationBindings = bindings.getOperationBinding("showComments");

            if (operationBindings != null) {
                operationBindings.getParamsMap().put("sourceType", sourceType);
                operationBindings.getParamsMap().put("sourceId", sourceId);
                operationBindings.execute();

            }
        }

        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        projectPopup.show(ph);
    }

    /**
     *
     * @param actionEvent
     */

    public void addProject(ActionEvent actionEvent) {
        ADFUtil.setEL("#{viewScope.operation}", "create");
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        projectPopup.show(ph);
    }

    /**
     *
     * @param actionEvent
     */
    public void deleteProject(ActionEvent actionEvent) {
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        deleteProjectPopup.show(ph);
    }


    /**
     *
     * @param dialogEvent
     */
    public void projectsDialog(DialogEvent dialogEvent) {
        Outcome outcome = dialogEvent.getOutcome();
        if (outcome.equals(Outcome.ok)) {
            String operation = (String)ADFContext.getCurrent().getViewScope().get("operation");

            ADFUtil.invokeEL("#{bindings.Commit.execute}");

            //Code for adding Project Owner as project admin in Members table
            if (operation != null && operation.equals("create")) {

                BindingContainer bindings = getBindings();
                OperationBinding operationBindings = bindings.getOperationBinding("addMember");
                operationBindings.execute();
            }

            //Code for Adding Comments
            BindingContainer bindings = getBindings();
            OperationBinding operationBindings = bindings.getOperationBinding("addComments");
            if (operationBindings != null) {
                operationBindings.getParamsMap().put("sourceType", "Projects");
                operationBindings.execute();
            }

            AdfFacesContext.getCurrentInstance().addPartialTarget(projectsTable);
        }
        ADFUtil.setEL("#{viewScope.operation}", "");
    }


    /**
     *
     * @param popupCanceledEvent
     */

    public void projectPopupClose(PopupCanceledEvent popupCanceledEvent) {

        String checkForRollBack = (String)ADFContext.getCurrent().getViewScope().get("operation");
        if (checkForRollBack != null && checkForRollBack.equals("create")) {
            DCBindingContainer bindings = (DCBindingContainer)getBindings();
            DCIteratorBinding dcIter = bindings.findIteratorBinding("ProjectsIterator");
            Row row = dcIter.getCurrentRow();
            OperationBinding operationBindings = bindings.getOperationBinding("rollBackForProjects");
            operationBindings.getParamsMap().put("r", row);
            operationBindings.execute();

        }
        ADFUtil.setEL("#{viewScope.operation}", "");
        AdfFacesContext.getCurrentInstance().addPartialTarget(projectsTable);
    }

    public void projectDialog(DialogEvent dialogEvent) {
        Outcome out = dialogEvent.getOutcome();
        if (out.equals(Outcome.ok)) {
            ADFUtil.setEL("#{viewScope.operation}", "");
            ADFUtil.invokeEL("#{bindings.Commit.execute}");
            AdfFacesContext.getCurrentInstance().addPartialTarget(projectsTable);
        }
    }

    /**
     *
     * @param dialogEvent
     */
    public void projectDeleteDialog(DialogEvent dialogEvent) {
        Outcome out = dialogEvent.getOutcome();
        if (out.equals(Outcome.ok)) {
            ADFUtil.invokeEL("#{bindings.Delete.execute}");
            ADFUtil.invokeEL("#{bindings.Commit.execute}");


        }
    }

    //Members

    /**
     *
     * @param actionEvent
     */
    public void updateMembers(ActionEvent actionEvent) {
        ADFUtil.setEL("#{viewScope.operation}", "update");
        RichPopup.PopupHints ph = new RichPopup.PopupHints();


        String sourceType = (String)ADFContext.getCurrent().getPageFlowScope().get("type");
        oracle.jbo.domain.Number sourceId =
            (oracle.jbo.domain.Number)ADFContext.getCurrent().getPageFlowScope().get("sourceId");
        if (sourceType != null && sourceId != null) {
            BindingContainer bindings = getBindings();
            OperationBinding operationBindings = bindings.getOperationBinding("showComments");

            if (operationBindings != null) {
                operationBindings.getParamsMap().put("sourceType", sourceType);
                operationBindings.getParamsMap().put("sourceId", sourceId);
                operationBindings.execute();

            }
        }

        membersPopup.show(ph);
        AdfFacesContext.getCurrentInstance().addPartialTarget(membersTable);

    }

    /**
     *
     * @param actionEvent
     */
    public void addMembers(ActionEvent actionEvent) {
        ADFUtil.setEL("#{viewScope.operation}", "create");
        ADFUtil.setEL("#{viewScope.type}", "Members");
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        membersPopup.show(ph);

    }


    /**
     *
     * @param actionEvent
     */
    public void deleteMember(ActionEvent actionEvent) {
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        deleteMemberPopup.show(ph);
    }

    /**
     *
     * @param dialogEvent
     */
    public void membersDialog(DialogEvent dialogEvent) {
        Outcome outcome = dialogEvent.getOutcome();
        if (outcome.equals(Outcome.ok)) {
            ADFUtil.setEL("#{viewScope.operation}", "");
            ADFUtil.invokeEL("#{bindings.Commit.execute}");

            BindingContainer bindings = getBindings();
            OperationBinding operationBindings = bindings.getOperationBinding("addComments");
            if (operationBindings != null) {
                operationBindings.getParamsMap().put("sourceType", "Members");
                operationBindings.execute();
            }
        }
    }

    /**
     *
     * @param popupCanceledEvent
     */
    public void memberPopupClose(PopupCanceledEvent popupCanceledEvent) {
        String checkForRollBack = (String)ADFContext.getCurrent().getViewScope().get("operation");
        if (checkForRollBack != null && checkForRollBack.equals("create")) {
            DCBindingContainer bindings = (DCBindingContainer)getBindings();
            DCIteratorBinding dcIter = bindings.findIteratorBinding("PrjMembersIterator");
            Row row = dcIter.getCurrentRow();
            OperationBinding operationBindings = bindings.getOperationBinding("rollBackForMembers");
            operationBindings.getParamsMap().put("r", row);
            operationBindings.execute();

        }
        ADFUtil.setEL("#{viewScope.operation}", "");

        ADFUtil.setEL("#{viewScope.operation}", "");

        ADFUtil.invokeEL("#{bindings.Rollback.execute}");
        AdfFacesContext.getCurrentInstance().addPartialTarget(membersTable);
    }

    /**
     *
     * @param dialogEvent
     */
    public void memberDeleteDialog(DialogEvent dialogEvent) {
        Outcome out = dialogEvent.getOutcome();
        if (out.equals(Outcome.ok)) {
            ADFUtil.invokeEL("#{bindings.Delete.execute}");
            ADFUtil.invokeEL("#{bindings.Commit.execute}");


        }
    }

    /**
     *
     * @param actionEvent
     */
    public void showComments(ActionEvent actionEvent) {
        RichPopup.PopupHints ph = new RichPopup.PopupHints();
        commentsPopup.show(ph);
    }


    public void enableOrDisableButton(ValueChangeEvent valueChangeEvent) {

        valueChangeEvent.getComponent().processUpdates(FacesContext.getCurrentInstance());
        DCBindingContainer bindings = (DCBindingContainer)getBindings();
        DCIteratorBinding dcIter = bindings.findIteratorBinding("ProjectsIterator");
        if (dcIter != null) {
            Row row = dcIter.getCurrentRow();
            //oracle.jbo.domain.Number prjId=(oracle.jbo.domain.Number)row.getAttribute("PrjId");
            oracle.jbo.domain.Number projectOwner = (oracle.jbo.domain.Number)row.getAttribute("ProjectOwner");
            Map sessionScope = ADFContext.getCurrent().getSessionScope();
            Long resourceId = (Long)sessionScope.get("resourceId");
            if (projectOwner != null && resourceId != null && projectOwner.intValue() == resourceId.intValue()) {

                ADFUtil.setEL("#{pageFlowScope.visibleStatus}", true);

            } else if (projectOwner != null && resourceId != null) {
                OperationBinding operationBindings = bindings.getOperationBinding("filterTask");
                operationBindings.getParamsMap().put("resourceId", resourceId);
                operationBindings.execute();
                ADFUtil.setEL("#{pageFlowScope.visibleStatus}", false);
            }
        }


    }

    public void setVisibleStatus() {
        ADFUtil.setEL("#{pageFlowScope.visibleStatus}", true);
    }
}
